//
//  HomeViewController.swift
//  MQLWeibo
//
//  Created by MQL-IT on 2016/10/23.
//  Copyright © 2016年 MQL-IT. All rights reserved.
//

import UIKit
//定义全局常量,尽量使用私有,仅在本文件中使用
private let cellId = "cellId"

class HomeViewController: BaseViewController {
    //懒加载微博数据数组,懒加载无需考虑实例化和解包;
    fileprivate lazy var statusList = [String]()
    
    //加载数据
    override func loadData() {
        //模拟延时加载数据
        // Argument labels '(deadline:, execute:, _:)' do not match any available overloads
        printLog(message: "开始加载数据")
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
        
            
            for i in 0..<20 {
                if self.isPullUp {
                    self.statusList.append("上拉\(i)")
                } else {
                    //将数据插入到数组的顶部
                    self.statusList.insert(i.description, at: 0)
                }
                
            }
            printLog(message: "加载数据结束")
            //加载数据结束,结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullUp = false
            //刷新表格
            self.tableView?.reloadData()
            
        }
        
    }
    
    //显示好友
    @objc fileprivate func showFriends() {
        print(#function)
        let vc = TestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}
//MARK: - 表格数据源方法,具体的数据源方法,不需要super
extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
         let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //3.返回cell
        return cell
    }
    
    
    
}
//MARK: - 设置界面
extension HomeViewController {
    //重写父类方法
    override func setupUI() {
        super.setupUI()
        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
}






